import { INIT_LIST_DATA, VAL_CHANGE, ADD_ITEM, DEL_ITEM } from './action-types';

const defaultState = {
  iptVal: '',
  listData: []
}
export default (state = defaultState, action) => {
  let newState = JSON.parse(JSON.stringify(state))
  console.log(action)
  switch (action.type) {
    case INIT_LIST_DATA:
      newState.listData = action.data
      return newState
    case VAL_CHANGE:
      newState.iptVal = action.data
      return newState
    case ADD_ITEM:
      if (action.data.trim()) {
        newState.listData.unshift(action.data)
      }
      newState.iptVal = ''
      return newState
    case DEL_ITEM:
      newState.listData.splice(action.data, 1)
      return newState
    default:
      return state
  }
}